package fragment.contact;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.menhoo.toollibrary.common.assist.Check;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.util.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import application.HttpUrlConstant;
import application.UIApplication;
import menhoo.com.i8officelibrary.R;
import model.EmployeeItemDBModel;
import model.SearchItemModel;

/**
 * Created by ThinkPad on 2017/5/24.
 */

public class ContactsSearchAdapter  extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    private BitmapUtils bitmapUtils;
    List<SearchItemModel> data;
    public  ContactsSearchAdapter(Context context, List<SearchItemModel> list){
        this.context = context;
        inflater = LayoutInflater.from(context);
        bitmapUtils = new BitmapUtils(context, UIApplication.getAppCachePath(context));
        data=list;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        SearchItemModel item = (SearchItemModel)getItem(position);

        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.adapter_item_address_book, null);
            viewHolder.img = (ImageView) convertView.findViewById(R.id.imageView1);
            viewHolder.checkBox = (ImageView) convertView.findViewById(R.id.imageViewCheck);
            viewHolder.content = (TextView) convertView.findViewById(R.id.textView1);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)viewHolder.img.getLayoutParams();
        lp.setMargins(10, 8, 0, 8);
        viewHolder.img.setLayoutParams(lp);
        ((View)viewHolder.img.getParent()).setBackgroundResource(R.drawable.selector_list);

        Map<String,String> parm= new HashMap<String,String>();
        parm.put("emplid", item.ID);
        bitmapUtils.display(viewHolder.img, HttpUrlConstant.getWebSiteFullUrl(HttpUrlConstant.doShowPhoto, parm));

        viewHolder.checkBox.setVisibility(View.GONE);

        viewHolder.content.setText(item.Title);

        return convertView;
    }

    class ViewHolder {
        TextView content;
        ImageView img;
        ImageView checkBox;
    }

    /**
     * 搜索
     * @param key  可多级搜索 空格为分隔符
     * @return
     */
    public static List<SearchItemModel> getSearchData(String key, List<EmployeeItemDBModel> listData)    {
        if(Check.isEmpty(key)){return null;}
        String[] keys = key.split(" ");
        List<SearchItemModel> re_list =null;
        LogUtils.e("搜索字段:"+keys.length);
        for(int i=0;i<keys.length;i++)
        {
            String searchKey=keys[i];
            LogUtils.e("搜索字段:"+(searchKey==null?"null":searchKey));
            if(i==0)
            {
                List<SearchItemModel> temp_list=new ArrayList<SearchItemModel>();
                for(EmployeeItemDBModel item : listData){
                    if(item.type==EmployeeItemDBModel.ITEM)
                    {
                        if((item.Name!=null&&item.Name.toLowerCase().indexOf(searchKey.toLowerCase())>=0 )||( item.DeptID!=null&&item.DeptID.toLowerCase().indexOf(searchKey.toLowerCase())>=0 )){
                            SearchItemModel sm = new SearchItemModel();
                            sm.ID=item.EmplID;
                            sm.Title=item.Name;

                            Map<String,String> parm= new HashMap<String,String>();
                            parm.put("emplid", item.EmplID);
                            sm.ImgUrl=HttpUrlConstant.getWebSiteFullUrl(HttpUrlConstant.doShowPhoto,parm);

                            sm.Entity=item;
                            temp_list.add(sm);
                        }
                    }
                }
                re_list=temp_list;
            }
            else
            {
                List<SearchItemModel> temp_list=new ArrayList<SearchItemModel>();
                for(SearchItemModel search_item : re_list)
                {
                    EmployeeItemDBModel item =(EmployeeItemDBModel)search_item.Entity;
                    if(item.Name.toLowerCase().indexOf(searchKey.toLowerCase())>=0 || item.DeptID.toLowerCase().indexOf(searchKey.toLowerCase())>=0 ){
                        temp_list.add(search_item);
                    }
                }
                re_list=temp_list;
            }
        }

        if(re_list!=null && re_list.size()>0){
            return re_list;
        }
        else{
            return null;
        }

    }


}
